package Dao.impl;

import Dao.EmployeeDao;
import Domin.Employee;
import Utils.ConnectionUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class EmployeeDaoImpl implements EmployeeDao {

    @Override
    public int insertEmployee(Employee emp) {
        String sql = "INSERT INTO employee(emp_name, department, position, role, password) VALUES(?, ?, ?, ?, ?)";
        try (Connection con = ConnectionUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            ps.setString(1, emp.getEmpName());
            ps.setString(2, emp.getDepartment());
            ps.setString(3, emp.getPosition());
            ps.setString(4, emp.getRole());
            ps.setString(5, emp.getPassword());
            int rows = ps.executeUpdate();
            if (rows > 0) {
                try (ResultSet rs = ps.getGeneratedKeys()) {
                    if (rs.next()) return rs.getInt(1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public Optional<Employee> findByEmpName(String empName) {
        String sql = "SELECT emp_id, emp_name, department, position, role, password FROM employee WHERE emp_name = ?";
        try (Connection con = ConnectionUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setString(1, empName);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Employee e = new Employee();
                    e.setEmpId(rs.getInt("emp_id"));
                    e.setEmpName(rs.getString("emp_name"));
                    e.setDepartment(rs.getString("department"));
                    e.setPosition(rs.getString("position"));
                    e.setRole(rs.getString("role"));
                    e.setPassword(rs.getString("password"));
                    return Optional.of(e);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    @Override
    public List<Employee> getAllEmployees() {
        List<Employee> list = new ArrayList<>();
        String sql = "SELECT emp_id, emp_name, department, position, role FROM employee";
        try (Connection con = ConnectionUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                Employee e = new Employee();
                e.setEmpId(rs.getInt("emp_id"));
                e.setEmpName(rs.getString("emp_name"));
                e.setDepartment(rs.getString("department"));
                e.setPosition(rs.getString("position"));
                e.setRole(rs.getString("role"));
                list.add(e);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean updateEmployee(Employee emp) {
        String sql = "UPDATE employee SET emp_name=?, department=?, position=? WHERE emp_id=?";
        try (Connection con = ConnectionUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setString(1, emp.getEmpName());
            ps.setString(2, emp.getDepartment());
            ps.setString(3, emp.getPosition());
            ps.setInt(4, emp.getEmpId());
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteEmployee(int empId) {
        String sql = "DELETE FROM employee WHERE emp_id = ?";
        try (Connection con = ConnectionUtil.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            ps.setInt(1, empId);
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}